﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Threading;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Common.Utilities.TextMatch;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Index;

namespace Workflows.Components.TextMining.FullTextSearch
{
    /// <summary>
    /// 
    /// </summary>
    public class LuceneIndexingWorker:IWorker 
    {
        #region Implementation of IWorker

        public event WorkerStarted WorkerStartedEvent;
        public event WorkerFinished WorkerFinishedEvent;
        public event WorkerHasError WorkerErredEvent;
        public event WorkerCancelled WorkerCancelledEvent;
        public event WorkerStatusChanged WorkerStatusChangedEvent;

        private Thread workerThread;
        private LuceneIndexingInstruction instruction;
        private LuceneIndexingOutput output;
        private RunContext _Context;
        private Guid _TicketID;
        private string _WorkflowName;
        private string _ComponentName;

        /// <summary>
        /// 
        /// </summary>
        public string ComponentName
        {
            get { return _ComponentName; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string WorkflowName
        {
            get { return _WorkflowName; }
        }

        /// <summary></summary>
        public Thread WorkerThread
        {
            get { return workerThread; }
        }
        /// <summary></summary>
        public InstructionBase Instruction
        {
            get { return instruction; }
        }
        /// <summary></summary>
        public ResultBase Output
        {
            get { return output; }
        }
        /// <summary></summary>
        public Type ComponentType
        {
            get { return typeof(LuceneIndexingComponent); }
        }
        /// <summary></summary>
        public RunContext Context
        {
            get { return _Context; }
            set { _Context = value; }
        }
        /// <summary></summary>
        public void SetInstruction(Guid ticketGuid, InstructionBase request, string workflowName, string componentName)
        {
            this._TicketID = ticketGuid;
            this.instruction = (LuceneIndexingInstruction)request;
            this._WorkflowName = workflowName;
            this._ComponentName = componentName;
        }
        /// <summary></summary>
        public void SetInstruction(RunContext context, Guid ticketGuid, InstructionBase request, string workflowName, string componentName)
        {
            this._TicketID = ticketGuid;
            this.instruction = (LuceneIndexingInstruction)request;
            this._Context = context;
            this._WorkflowName = workflowName;
            this._ComponentName = componentName;
        }
        /// <summary></summary>
        public void DoWork()
        {
            if (this.WorkerStartedEvent != null)
            {
                this.WorkerStartedEvent(this._TicketID, this, this.GetType(), this.instruction);
            }
            this.workerThread = Thread.CurrentThread;
            this.IndexTextFields();
        }
        /// <summary></summary>
        public void DoWorkAsync()
        {
            if (this.WorkerStartedEvent != null)
            {
                this.WorkerStartedEvent(this._TicketID, this, this.GetType(), this.instruction);
            }
            this.workerThread = new Thread(new ThreadStart(this.IndexTextFields));
            this.workerThread.Start();
        }
        /// <summary></summary>
        public void CancelWork()
        {
            if (this.workerThread != null && this.workerThread.IsAlive)
            {
                this.workerThread.Abort();
                this.workerThread.Join();

                if (this.WorkerCancelledEvent != null)
                {
                    this.WorkerCancelledEvent(this._TicketID, this.GetType(), this.instruction);
                }
            }
        }

        #endregion

        #region work
        private void IndexTextFields()
        {
            SqlConnection conn=new SqlConnection(this.instruction.ConnStr);
            bool createNewIndexFile = true;
            if(Directory.Exists(this.instruction.IndexingOutputFilePath))
            {
                createNewIndexFile = false;
            }
            StopWordsUtil stopWordUtil=new StopWordsUtil(StopWordsUtil.StopWordList.PubmedStopWordList);
            List<string> stopWords = stopWordUtil.StopWords;
            IndexWriter indexWriter = new IndexWriter(
                this.instruction.IndexingOutputFilePath, 
                new StandardAnalyzer(stopWords.ToArray()),
                createNewIndexFile);
            
            int recordCount = DbUtil.GetTableRecordCount(
                DataProviderType.MSSQL,
                this.instruction.ConnStr, this.instruction.SqlStatement);
            try
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = this.instruction.SqlStatement;
                SqlDataReader reader = cmd.ExecuteReader();
                int docIndexed = 0;
                while(reader.Read())
                {
                    if (reader[this.instruction.KeyField] == null || reader[this.instruction.KeyField] == DBNull.Value)
                        continue;

                    Document luceneDoc=new Document();
                    Field idField=new Field(
                        this.instruction.KeyField,
                        reader[this.instruction.KeyField].ToString(),
                        Field.Store.YES,
                        Field.Index.NO);
                    luceneDoc.Add(idField);

                    foreach(string fieldName in this.instruction.ContentFields)
                    {
                        if (reader[fieldName] != null && reader[fieldName]!=DBNull.Value)
                        {
                            string fieldContent = reader[fieldName].ToString();
                            Field contentField = new Field(
                                fieldName,
                                fieldContent,
                                Field.Store.YES,
                                Field.Index.TOKENIZED);
                            luceneDoc.Add(contentField);
                        }
                    }

                    indexWriter.AddDocument(luceneDoc);

                    docIndexed++;
                    if(this.WorkerStatusChangedEvent !=null)
                    {
                        string msg = string.Format("Indexing {0} of {1} records...", docIndexed, recordCount);
                        int pct = docIndexed*100/recordCount;
                        this.WorkerStatusChangedEvent(
                            this._TicketID, this.GetType(), this.instruction, this._WorkflowName, this._ComponentName,
                            msg, pct);
                    }
                }
                reader.Close();

                if(this.WorkerStatusChangedEvent !=null)
                {
                    string msg = "Optimizing index...";
                    int pct = 50;
                    this.WorkerStatusChangedEvent(
                        this._TicketID, this.GetType(), this.instruction, this._WorkflowName, this._ComponentName, msg,
                        pct);
                }
                indexWriter.Optimize();
                indexWriter.Close();
                indexWriter = null;

                this.output=new LuceneIndexingOutput(
                    this.instruction.OwnerComponentID,this._TicketID,
                    this.instruction.IndexingOutputFilePath);

                if (this.WorkerFinishedEvent != null)
                    this.WorkerFinishedEvent(this._TicketID, this.GetType(), this.instruction, this.output);
            }
            catch (Exception ex)
            {
                if (this.WorkerErredEvent != null)
                    this.WorkerErredEvent(this._TicketID, this.GetType(), this.instruction, ex.ToString(), true);
            }
            finally
            {
                conn.Close();

                if(indexWriter !=null)
                {
                    indexWriter.Close();
                    indexWriter = null;
                }
            }
        }
        #endregion
    }
}
